#include <jni.h>
#include <string>
#include "TreadMillCommandUtil.h" // 包含你的命令工具头文件

extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_parseCommand(JNIEnv *env, jobject /* this */, jstring command) {
    // 将 Java 字符串转换为 C++ 字符串
    const char *commandChars = env->GetStringUTFChars(command, nullptr);

    // 调用 C++ parseCommand 函数
    std::string result = TreadMillCommandUtil::parseCommand(commandChars);

    // 释放 Java 字符串
    env->ReleaseStringUTFChars(command, commandChars);

    // 将 C++ 字符串转换为 Java 字符串并返回
    return env->NewStringUTF(result.c_str());
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_buildCommandStringData(JNIEnv *env, jobject /* this */, jstring header, jstring dataType, jstring data) {
    // 将 Java 字符串转换为 C++ 字符串
    const char *headerChars = env->GetStringUTFChars(header, nullptr);
    const char *dataTypeChars = env->GetStringUTFChars(dataType, nullptr);
    const char *dataChars = env->GetStringUTFChars(data, nullptr);

    // 调用 C++ 函数
    std::string commandString = TreadMillCommandUtil::BuildCommandStringData(headerChars, dataTypeChars, dataChars);

    // 释放 Java 字符串
    env->ReleaseStringUTFChars(header, headerChars);
    env->ReleaseStringUTFChars(dataType, dataTypeChars);
    env->ReleaseStringUTFChars(data, dataChars);

    // 将 C++ 字符串转换为 Java 字符串并返回
    return env->NewStringUTF(commandString.c_str());
}

// 1. InitCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_initCommand(JNIEnv *env, jobject /* this */) {
    std::string commandString = TreadMillCommandUtil::InitCommand();
    return env->NewStringUTF(commandString.c_str());
}

// 2. SettingCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_settingCommand(JNIEnv *env, jobject /* this */, jint set) {
    std::string commandString = TreadMillCommandUtil::SettingCommand(set);
    return env->NewStringUTF(commandString.c_str());
}

// 3. ResetCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_resetCommand(JNIEnv *env, jobject /* this */) {
    std::string commandString = TreadMillCommandUtil::ResetCommand();
    return env->NewStringUTF(commandString.c_str());
}

// 4. SetInclineCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_setInclineCommand(JNIEnv *env, jobject /* this */, jfloat incline) {
    std::string commandString = TreadMillCommandUtil::SetInclineCommand(incline);
    return env->NewStringUTF(commandString.c_str());
}

// 5. CalibrateCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_calibrateCommand(JNIEnv *env, jobject /* this */) {
    std::string commandString = TreadMillCommandUtil::CalibrateCommand();
    return env->NewStringUTF(commandString.c_str());
}

// 6. DiagnosticsCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_diagnosticsCommand(JNIEnv *env, jobject /* this */, jint option) {
    std::string commandString = TreadMillCommandUtil::DiagnosticsCommand(option);
    return env->NewStringUTF(commandString.c_str());
}

// 7. StateChangedCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_stateChangedCommand(JNIEnv *env, jobject /* this */) {
    std::string commandString = TreadMillCommandUtil::StateChangedCommand();
    return env->NewStringUTF(commandString.c_str());
}

// 8. ErrorDataCommand JNI 函数
extern "C"
JNIEXPORT jstring JNICALL
Java_com_oem_qisda_TreadmillAPI_errorDataCommand(JNIEnv *env, jobject /* this */) {
    std::string commandString = TreadMillCommandUtil::ErrorDataCommand();
    return env->NewStringUTF(commandString.c_str());
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_oem_qisda_TreadmillAPI_getState(JNIEnv *env, jobject /* this */) {
    // 调用 TreadMillCommandUtil 获取跑步机状态
    return TreadMillCommandUtil::getTreadmillState();
}

